import Vue from 'vue'
import VueRouter from 'vue-router'
import App from '@/App.vue'
import routes from '@/router'
import store from '@/vuex/store'
/* eslint-disable no-new */
Vue.use(VueRouter)
let router = new VueRouter({
  // mode: 'history',
  base: __dirname,
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  },
  routes
})

router.beforeEach((to, form, next) => {
  let currentParams = to.params && to.params.id ? to.params.id : ''
  if (currentParams && currentParams === 'new' && to.meta.bread[2]) {
    to.meta.bread[2].title = `新增${to.meta.bread[2].title.slice(2)}`
  } else if (currentParams && currentParams === 'look' && to.meta.bread[2]) {
    to.meta.bread[2].title = `查看${to.meta.bread[2].title.slice(2)}`
  } else if (currentParams && currentParams !== 'new' && currentParams !== 'look' && to.meta.bread[2]) {
    to.meta.bread[2].title = `编辑${to.meta.bread[2].title.slice(2)}`
  }
  // to.meta.bread[2].title = this.pageType !== 'new' ? '编辑公司' : '新建公司'
  // let user = Vue.localStorage.get(Vue.const.COOKIE.KEY_USER)
  // let token = Vue.localStorage.get(Vue.const.COOKIE.KEY_TOKEN)
  // if (token) {
  //   store.commit('UPDATE_USER', {
  //     user: user,
  //     token: token
  //   })
  // }
  // console.log(to.meta.toLogin)
  // console.log(token)
  // if (!to.meta.toLogin && !token) {
  //   return next({name: 'login'})
  // }
  next()
})
export default {
  run () {
    new Vue({
      render (h) {
        return h(App)
      },
      store,
      router,
      data: {
        event: new Vue()
      }
    }).$mount('#app')
  }
}
export { router }
